package com.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.entity.Orders;
import com.hotel.mapper.OrdersMapper;
import com.hotel.service.OrderDetailService;
import com.hotel.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Lin
 * @since 2022-06-13
 */
@Service
public class OrdersServiceImp extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private OrderDetailService orderDetailService;

    @Override
    public IPage<Orders> getOrderList(String number, String nickName, String checkoutTime, Integer status, Integer current, Integer size) {
        List<Orders> ordersList = ordersMapper.getOrdersList(number,nickName,checkoutTime,status);
        List pageList = new ArrayList<>();
        int curIdx = current > 1 ? (current - 1) * size : 0;
        for (int i = 0; i < size && curIdx + i < ordersList.size(); i++) {
            pageList.add(ordersList.get(curIdx + i));
        }
        IPage page = new Page<>(current, size);
        page.setRecords(pageList);
        page.setTotal(ordersList.size());
        return page;
    }

    @Override
    public List<Orders> getOrderDetail(String id) {
        return ordersMapper.getOrderDetail(id);
    }

    @Override
    public int deleteOrder(String orderNumber) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("number",orderNumber);
        wrapper.eq("status",5);

        QueryWrapper<Orders> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("number",orderNumber);
        Orders orders = this.baseMapper.selectOne(wrapper1);

        int delete = this.baseMapper.delete(wrapper);
        if (delete>0&&orders!=null){
            orderDetailService.deleteOrderDetail(String.valueOf(orders.getId()));
        }

        return delete;
    }

    @Override
    public int updateOrderRemark(String orderNumber,String remark) {
        UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.eq("number",orderNumber);
        wrapper.set("remark",remark);
        return this.baseMapper.update(null,wrapper);
    }

    @Override
    public int updateOrderStatus(String orderNumber) {
        UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.eq("number",orderNumber);
        wrapper.set("status","3");
        return this.baseMapper.update(null,wrapper);
    }

    @Override
    public List<Orders> getUnpaidOrder() {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<Orders> orders = this.baseMapper.selectList(wrapper);
        return orders;
    }

    @Override
    public int deleteByOrderNumber(String orderNumber) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("number",orderNumber);
        return this.baseMapper.delete(wrapper);
    }




}
